<?php

/**
 * 回款业绩仓库
 * @author wangwx 2015-5-12
 */

namespace common\repositories\myhtsq;

use common\repositories\BaseRepository;
use common\support\StringHelper;
use common\entities\myhtsq\PaymentAchievementEntity;
use common\models\myhtsq\PaymentAchievementModel;

class PaymentAchievementRepository extends BaseRepository {

    /**
     * 构造方法
     * @param unknown $config (optional)
     */
    public function __construct($config = []) {
        parent::__construct($config);
    }

    /**
     * 根据合同id,获取合同回款业绩列表
     * @param string $contractId 合同id
     * @return array
     */
    public static function getPaymentAchievementListByContractId($contractId) {
        return PaymentAchievementEntity::find()->where(['contract_id' => $contractId])->asArray()->all();
    }

    /**
     * 根据回款签约id,删除回款业绩
     * @param string $achievementId 签约id
     * @return boolean
     */
    public static function deletePaymentAchievementByAchievementId($achievementId) {
        return PaymentAchievementEntity::deleteAll(['achievement_id' => $achievementId]);
    }

    /**
     * 新增回款业绩
     * @param common\models\myhtsq\PaymentAchievementModel $paymentAchievementModel 回款业绩模型
     * @return boolean|string
     */
    public static function addPaymentAchievement(PaymentAchievementModel $paymentAchievementModel) {
        $paymentAchievementEntity = new PaymentAchievementEntity();

        $data = $paymentAchievementModel->attributes;
        $paymentAchievementEntity->load($data, '');
        $paymentAchievementEntity->achievement_id = StringHelper::uuid();
        if ($paymentAchievementEntity->save()) {
            return $paymentAchievementEntity->achievement_id;
        } else {
            return false;
        }
    }

    /**
     * 更新回款业绩
     * @param common\models\myhtsq\PaymentAchievementModel $paymentAchievementModel 回款业绩模型
     * @return string|boolean
     */
    public static function updatePaymentAchievement(PaymentAchievementModel $paymentAchievementModel) {
        $paymentAchievementEntity = new PaymentAchievementEntity();
        $paymentAchievementModelInfo = $paymentAchievementEntity::find()->where(['achievement_id' => $paymentAchievementModel->achievement_id])->one();
        $data = $paymentAchievementModel->attributes;

        $paymentAchievementModelInfo->load($data, '');
        if ($paymentAchievementModelInfo->save()) {
            return $paymentAchievementModelInfo->achievement_id;
        } else {
            return false;
        }
    }

    /**
     * 根据回款与发票及应收款对应关系ID,删除回款业绩
     * @param string $invoicePaymentId 回款与发票及应收款对应关系ID
     * @return boolean|int
     */
    public static function deletePaymentAchievementByInvoicePaymentId($invoicePaymentId) {
        return PaymentAchievementEntity::deleteAll(['invoice_payment_id' => $invoicePaymentId]);
    }

    /**
     * 根据回款与发票及应收款对应关系ID,删除回款业绩
     * @param string $payment_id 回款ID
     * @return boolean|int
     */
    public static function deletePaymentAchievementByPaymentId($payment_id) {
        return PaymentAchievementEntity::deleteAll(['payment_id' => $payment_id]);
    }
}
